package com.wenlin.myproduction.binder;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wenlin.myproduction.R;
import com.wenlin.myproduction.bean.PlayerBean;
import com.wenlin.myproduction.util.UI;

import me.drakeet.multitype.ItemViewBinder;

public class PlayerBinder extends ItemViewBinder<PlayerBean, PlayerBinder.VH> {
    private OnClickListener onClickListener;

    public PlayerBinder(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @NonNull
    @Override
    protected VH onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new VH(inflater.inflate(R.layout.item_player, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull VH holder, @NonNull PlayerBean item) {
        UI.setText(holder.tvNum, item.getNum() > 0 ? item.getNum() + "" : "");
        UI.setText(holder.tvName, item.getName());
        UI.setText(holder.tvStar, item.getStar());
        UI.setImageResource(holder.iv, item.isChecked() ? R.mipmap.icon_free_select : R.mipmap.icon_free_normal);
        UI.setOnLongClickListener(holder.itemView, v -> {
            if (onClickListener != null) onClickListener.onLongClick(item);
            return true;
        });
        UI.setOnClickListener(holder.itemView, v -> {
            if (onClickListener != null) onClickListener.onClickCheck(item);
        });
    }

    static class VH extends RecyclerView.ViewHolder {
        TextView tvNum, tvName, tvStar;
        ImageView iv;

        public VH(@NonNull View itemView) {
            super(itemView);
            tvNum = itemView.findViewById(R.id.tv_num);
            tvName = itemView.findViewById(R.id.tv_name);
            tvStar = itemView.findViewById(R.id.tv_star);
            iv = itemView.findViewById(R.id.iv);
        }
    }

    public interface OnClickListener {
        void onLongClick(PlayerBean bean);

        void onClickCheck(PlayerBean bean);
    }
}
